package cn.seocoo.platform.validatecode.service.impl;

import cn.seocoo.platform.common.constant.ForegroundUserConstant;
import cn.seocoo.platform.common.core.text.Convert;
import cn.seocoo.platform.common.utils.SendShortMessageUtil;
import cn.seocoo.platform.common.utils.StringUtils;
import cn.seocoo.platform.validatecode.domain.Validatecode;
import cn.seocoo.platform.validatecode.mapper.ValidatecodeMapper;
import cn.seocoo.platform.validatecode.service.ValidatecodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

/**
 * [验证码] 服务层实现
 *
 * @author cdw
 * @date 2019-10-21
 */
@Service
public class ValidatecodeServiceImpl implements ValidatecodeService
{
	@Autowired
	private ValidatecodeMapper validatecodeMapper;

	/**
	 * 查询[验证码]信息
	 *
	 * @param id [验证码]ID
	 * @return [验证码]信息
	 */
	@Override
	public Validatecode selectValidatecodeById(Integer id)
	{
		return validatecodeMapper.selectValidatecodeById(id);
	}

	/**
	 * 查询[验证码]列表
	 *
	 * @param validatecode [验证码]信息
	 * @return [验证码]集合
	 */
	@Override
	public List<Validatecode> selectValidatecodeList(Validatecode validatecode)
	{
		return validatecodeMapper.selectValidatecodeList(validatecode);
	}

	/**
	 * 新增[验证码]
	 *
	 * @param validatecode [验证码]信息
	 * @return 结果
	 */
	@Override
	public int insertValidatecode(Validatecode validatecode)
	{
		return validatecodeMapper.insertValidatecode(validatecode);
	}

	/**
	 * 修改[验证码]
	 *
	 * @param validatecode [验证码]信息
	 * @return 结果
	 */
	@Override
	public int updateValidatecode(Validatecode validatecode)
	{
		return validatecodeMapper.updateValidatecode(validatecode);
	}

	/**
	 * 删除[验证码]对象
	 *
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteValidatecodeByIds(String ids)
	{
		return validatecodeMapper.deleteValidatecodeByIds(Convert.toStrArray(ids));
	}

	@Override
	public int getValidatecode(String phone)throws Exception {
		//生成随机验证码
		String validateCode = (int) (Math.random() * 1000 + 1000) + "";
		String text = ForegroundUserConstant.SIGN_SHANGGUO + ForegroundUserConstant.VALIDATECODE_MESSAGE + validateCode + ForegroundUserConstant.END_MESSAGE;
		//设置您要发送的内容
//      		String text = "【企业发展促进中心】您的认证信息审核通过，请前往创建参赛项目" + validateCode;
		int msg = 0;
		if (!StringUtils.isEmpty(phone)) {
			Validatecode validatecode = new Validatecode();
			validatecode.setPhone(phone);
			List<Validatecode> vList = validatecodeMapper.selectValidatecodeList(validatecode);
			validatecode.setValidateCode(validateCode);


			//有效期计算
			Calendar cal = Calendar.getInstance();

			if (vList.size() > 0) {
				//如果存在  更新

				validatecode = vList.get(0);
				validatecode.setValidateCode(validateCode);
				validatecode.setCreateTime(cal.getTime());
				cal.add(Calendar.MINUTE, +3);
				validatecode.setValidityTime(cal.getTime());

				validatecodeMapper.updateValidatecode(validatecode);
			} else {
				//如果不存在  插入
				cal.add(Calendar.MINUTE, +3);
				validatecode.setValidityTime(cal.getTime());
				validatecode.setCreateTime(cal.getTime());
				validatecodeMapper.insertValidatecode(validatecode);
			}
			try {
				SendShortMessageUtil.sendSms(text, phone);
				msg = 1;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return msg;
	}

	/**
	 * @Author pyh
	 * @Description ceshi 检查手机验证码是否正确
	 * @Date 2019/10/24
	 * @Param [validatecode]
	 * @return int:false-错误；true-正确
	 **/
	@Override
	public boolean checkValidatecode(Validatecode validatecode) {
		List<Validatecode> validatecodeList = validatecodeMapper.selectValidatecodeList(validatecode);
		if (validatecodeList == null || validatecodeList.size() == 0) {
			return false;
		}
		return true;
	}

}
